import Vue from 'vue'
import App from './App.vue'
import router from './router'

//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)

//导入格式化时间的插件
import moment from 'moment'
//定义全局过滤器
Vue.filter("dateFormat",function(dataStr,pattern="YYYY-DD-MM HH:mm:ss"){
  return moment(dataStr).format(pattern);
})

//每次刚进入网站，肯定会调用main.js，在刚调用的时候，先从本地存储中把购物车的数据读出来，放到store中
// var car = [];
// var obj = JSON.parse(localStorage.getItem("car"));
// if(obj instanceof Array){
//   car = obj;
// }else{
//   car.push(obj);
// }
var car = JSON.parse(localStorage.getItem("car") || "[]");
// var car = [];
// car.push(car1);
// console.log(car);
var store = new Vuex.Store({
  state: {
    car: car//将购物车中商品的数据用一个数组存储起来，里面是一个数据对象{id:id,count:数量，price:单价，selected: true是否选中}
  },
  mutations: {//this.$store.commit("","")
    addToCar(state, goodsinfo) {
      //console.log(state.car);
      console.log(state.car.some);
      var flag = false;//假设购物车中没有找到对应商品
      if(state.car != []){
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count += Number.parseInt(goodsinfo.count);
          flag = true;
          return true;
        }
      })
      }

      if (!flag) {
        state.car.push(goodsinfo);
      }
      //console.log(state.car);
      //更新car之后，把car存储到本地localStorage
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    updateGoodsInfo(state, goodsinfo) {
      //修改购物车中商品数量值
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = Number.parseInt(goodsinfo.count);
          return true;
        }
      })
      //当修改完商品数量，保存最新数据到localStorage
      localStorage.setItem("car", JSON.stringify(goodsinfo));
    },
    remove(state, id) {
      state.car.some((item, index) => {
        if (item.id == id) {
          state.car.splice(index, 1);
        }
      })
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    updateGoodsSelected(state, info) {
      state.car.some(item => {
        if (item.id == info.id) {
          item.selected = info.selected;
          return true;
        }
      })
      localStorage.setItem("car", JSON.stringify(state.car));
    }
  },
  getters: {//相当于过滤器filter,计算属性computed
    getAllCount(state) {
      var c = 0;
      if (state.car.forEach) {
        state.car.forEach(item => {
          c += item.count;
        })
      }

      return c;
    },
    getGoodsCount(state) {
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.count;
      });
      return o;
    },
    getGoodsSelected(state) {
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.selected;
      })
      return o;
    },
    getGoodsCountAndAmount(state) {
      var o = {
        count: 0,   //勾选数量
        amount: 0   //勾选总价
      }
      state.car.forEach(item => {
        if (item.selected) {
          o.count += item.count;
          o.amount += item.price * item.count;
        }
      })
      return o;
    }
  }
})


import axios from 'axios'
Vue.prototype.$axios = axios;
//导入MUI样式,注意
import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'

//引入图片插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//按需导入Mint-UI中的组件
import { Header, Swipe, SwipeItem, Button, Lazyload, Switch } from 'mint-ui'


Vue.component(Switch.name, Switch);
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
import 'mint-ui/lib/style.css'

Vue.use(Lazyload);

//定义全局的过滤器
Vue.filter('dataFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {

})


//Vue.use(Mint)
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
